//
//  LJScrollViewImage.m
//  LJScrollImageView
//
//  Created by lanou on 15/8/1.
//  Copyright (c) 2015年 lanou. All rights reserved.
//
#define kWidth (self.frame.size.width)
#define kHeight (self.frame.size.height)
#import "LJScrollView.h"

@interface LJScrollView ()<UIScrollViewDelegate>

@end

@implementation LJScrollView

- (id)initWithFrame:(CGRect)frame
          viewArray:(NSArray *)viewArray
          Animation:(NSTimeInterval)timeInterval{
    if (self = [super initWithFrame:frame]) {
        _scrollView = [[UIScrollView alloc] initWithFrame:frame];
        _leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        _centerView = [[UIView alloc] initWithFrame:CGRectMake(kWidth, 0, kWidth, kHeight)];
        _rightView = [[UIView alloc] initWithFrame:CGRectMake(2 * kWidth, 0, kWidth, kHeight)];
        [_scrollView addSubview:_leftView];
        [_scrollView addSubview:_centerView];
        [_scrollView addSubview:_rightView];
        _scrollView.contentSize = CGSizeMake(3 * kWidth, kHeight);
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.contentOffset = CGPointMake(kWidth, 0);
        self.viewArray = viewArray;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self addGestureRecognizer:tap];
        _scrollView.delegate = self;
        [self addSubview:_scrollView];
        [self reloadView];
         self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(kWidth - self.viewArray.count * 20, kHeight - 30, self.viewArray.count * 20, 30)];
        self.pageControl.numberOfPages = self.viewArray.count;
        [self addSubview:self.pageControl];
        [self pageChange];
        [self setupTimer:timeInterval];
        self.timeInterval = timeInterval;
    }
    return self;
}


//初始化计时器
- (void)setupTimer:(NSTimeInterval)timeInterval{
    
    if(timeInterval != 0){
        self.timer = [NSTimer timerWithTimeInterval:timeInterval target:self selector:@selector(timeChanged) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
}


- (void)timeChanged{

    self.pageControl.currentPage = (self.pageControl.currentPage + 1) % self.pageControl.numberOfPages;
    [UIView animateWithDuration:0.2 animations:^{
        self.scrollView.contentOffset = CGPointMake(2 * kWidth, 0);
    } completion:^(BOOL finished) {
        [self reloadView];
        [_scrollView setContentOffset:CGPointMake(kWidth, 0) animated:NO];
    }];
}

- (void)pageChange{

    self.pageControl.currentPage = self.currentViewIndex ;
}


- (void)reloadView{

    NSUInteger leftViewIndex, rightViewIndex;
    CGPoint offset = [_scrollView contentOffset];
    //向右滑动的时候,当前显示的view下标加1,如果是数组的最大值就变成0
    if (offset.x == 2 * kWidth) {
        _currentViewIndex = (_currentViewIndex + 1) % [self.viewArray count];
    }
    if (offset.x == 0){
        //如果为0,向左划的话因为不能为负数,所以直接我们手动赋值为
        if (_currentViewIndex == 0) {
            _currentViewIndex = [self.viewArray count] - 1;
        }
        else
        _currentViewIndex = (_currentViewIndex - 1) % [self.viewArray count];
    }
    //移除所有子视图
    [_centerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //重新设置中间视图显示视图
    [_centerView addSubview:_viewArray[_currentViewIndex]];
    
    //重新设置左右视图显示视图
     //如果为0,左视图下标不能为负数,所以直接赋值
    if (_currentViewIndex == 0) {
        leftViewIndex = [self.viewArray count] - 1;
    }
    else{
        leftViewIndex = (_currentViewIndex - 1) % [self.viewArray count];
    }
    rightViewIndex = (_currentViewIndex + 1) % (self.viewArray.count);
    //NSLog(@"%d  %d  %d",leftViewIndex,_currentViewIndex,rightViewIndex);
    [_leftView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_leftView addSubview:_viewArray[leftViewIndex]];
    [_rightView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_rightView addSubview:_viewArray[rightViewIndex]];
}


//开始拖拽的时候关闭计时器
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    [self.timer invalidate];
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    NSLog(@"滑动结束");
    //重新加载视图
    [self reloadView];
    //移回中间
    [_scrollView setContentOffset:CGPointMake(kWidth, 0) animated:NO];
    [self setupTimer:self.timeInterval];
    [self pageChange];
    
}


//在scrollView什么添加了轻拍手势
- (void)tap:(UITapGestureRecognizer *)tap{

    if (self.delegate != nil) {
           [self.delegate scrollViewDidEndSelected:self viewArray:self.viewArray atIndex:self.currentViewIndex];
    }
}
@end
